package com.jackyzgy.test.socket.copy2;

import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;


public class Server {
	public Server() {
	}

	public static void main(String[] args) {
		int port = 5000;// 监听端口
		try {
			ServerSocket ss = new ServerSocket(port);
			while (true) {
				Socket skt = ss.accept();
				System.out.println("recive connect request...");
				new process(skt);// 接收到一个连接后启动一个处理线程来处理
			}
		} catch (IOException ex) {
		}
	}
}

class process extends Thread {
	private Socket skt;

	public process(Socket skt) {
		this.skt = skt;
		this.start();
	}

	public void run() {
//		InputStream is = null;
		PrintStream os = null;
		
		try {
			while (true) {
//				is = skt.getInputStream();
				skt.setKeepAlive(true);
//				BufferedReader bis = new BufferedReader(new InputStreamReader(is));
//				int i;
				while (true) {
//					i = bis.read();
//					if (i == 13 || i == 10) {
//						break;
//					}
					os = new PrintStream(skt.getOutputStream());
					os.println("fgdsfdsgf");
					os.flush();
					
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (IOException ex) {
			try {
//				is.close();
				os.close();
				skt.close();
			} catch (IOException ex1) {
			}
		}
	}
}